package cz.zcu.fav.rat.managed.converter;

import java.util.Iterator;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import cz.zcu.fav.rat.beans.ObjectClass;
import cz.zcu.fav.rat.dataaccess.service.ObjectClassService;
import cz.zcu.fav.rat.managed.bean.SpringBeanFactory;

@FacesConverter(value = "objectClassConverter")
public class ObjectClassConverter implements Converter
{
	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String value)
	{
		ObjectClassService service = SpringBeanFactory.getInstance()
				.<ObjectClassService> getBean("objectClassService",
						ObjectClassService.class);
		Iterator<ObjectClass> it = service.getObjectClassByName(value)
				.iterator();
		if (it.hasNext())
		{
			return it.next();
		}
		return null;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value)
	{
		return ((ObjectClass) value).getName();
	}
}
